package com.lxp.test;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.lxp.base.adapter.DataBindingAdapter;
import com.lxp.base.base.BaseActivity;
import com.lxp.base.log.Log;
import com.lxp.test.contract.TestContract;
import com.lxp.test.databinding.ActivityMainBinding;
import com.lxp.test.databinding.ItemCarBinding;
import com.lxp.test.entity.CarInfo;
import com.lxp.test.entity.CarInfo.DataBean.RowsBean;
import com.lxp.test.presenter.TestPresenter;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding> implements TestContract.View {

    private CarInfo mCarInfo = new CarInfo();
    private TestPresenter mTestPresenter;

    private DataBindingAdapter<RowsBean, ItemCarBinding> mAdapter;
    private List<RowsBean> mCars = new ArrayList<>();

    @Override
    public void widgetClick(View v) {
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(View view) {
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mTestPresenter = new TestPresenter(this);

        mAdapter = new DataBindingAdapter<RowsBean, ItemCarBinding>(R.layout.item_car, mCars) {
            @Override
            public void onBindView(ItemCarBinding binding, RowsBean item) {
                binding.setRowsBean(item);
            }
        };
        mBinding.rv.setAdapter(mAdapter);

        mBinding.sw.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                if (direction == SwipyRefreshLayoutDirection.TOP) {
                    mTestPresenter.getCars();
                }
            }
        });

    }

    @SuppressLint("WrongConstant")
    @Override
    public void doBusiness() {
        mBinding.setCarInfo(mCarInfo);
        mBinding.setMTestPresenter(mTestPresenter);

        Log.e(mContextView.getVisibility()+"=================");
    }


    @Override
    public void getCars(CarInfo info) {
        mBinding.sw.setRefreshing(false);
        mBinding.setCarInfo(info);
        mAdapter.setNewData(info.getData().getRows());
    }
}
